<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginFrom"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎使用{{ title }}</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginFrom.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              show-password
              v-model="loginFrom.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user.ts'
import { useRoute, useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
let loginFrom = reactive({ username: '', password: '' })
let title = ref(import.meta.env.VITE_APP_TITLE)
const userStore = useUserStore()
const $router = useRouter()
const $route = useRoute()
const loading = ref(false)
// 定义表单元素
let loginForms = ref()

// 登录方法
const login = async () => {
  // 点击登录按钮以后干什么
  // 通知仓库发起请求
  // 请求成功→路由跳转
  // 请求失败→弹出错误提示
  await loginForms.value.validate()
  loading.value = true
  try {
    await userStore.userLogin(loginFrom)

    let redirect: any = $route.query.redirect
    // 编程式导航转到展示数据的首页
    $router.push({ path: redirect || '/' })
    // 登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '登录成功!',
      title: `Hi,${getTime()}`,
    })
    // 登录成功，加载效果也消失
    loading.value = false
  } catch (error: any) {
    console.log(error)
    // 登录失败的提示信息
    ElNotification({
      type: 'error',
      message: '用户名或密码错误',
    })
    loading.value = false
  }
}

// 自定义校验规则函数
const validatorUserName = (_rule: any, value: any, callback: any) => {
  // rule 校验规则
  // value 表单的值
  // callback 回调函数,符合条件,callback放行通过,不符合:注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('用户名长度在5-10之间'))
  }
}
const validatorPassword = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度不能小于6位'))
  }
}
// 定义表单校验规则
const rules = {
  username: [
    // {
    //   required: true,
    //   min: 5, //min 是最小长度
    //   max: 10, //max 是最大长度
    //   message: '用户名长度在5-10之间', //message 错误提示信息
    //   trigger: 'blur', //trigger 触发校验的时机
    // },
    { validator: validatorUserName, trigger: 'change' },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 10,
    //   message: '密码长度在6-10之间',
    //   trigger: 'change',
    // },
    { validator: validatorPassword, trigger: 'change' },
  ],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.png') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    // background: url('@/assets/images/login_form.png') no-repeat;
    // background-size: cover;
    padding: 40px;
    border-radius: 20px;
    background-color: rgba(0, 0, 0, 0.5);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
